import {copyArr} from './arrayUtil'

var tableData;
var project_merageArr;  //第一列进行合并操作时存放的数组变量
var project_meragePos;  //上面的数组的下标值
var step_merageArr;     //第二列进行合并操作时存放的数组变量
var step_meragePos;    //上面的数组的下标值

/**
 * initData
 */
export function init(){
    tableData = [];
    //初始化数据函数
    project_merageArr = [];
    project_meragePos = 0;
    step_merageArr = [];
    step_meragePos = 0;
}

/**
 * element-ui 合并列的方法
 */
export function objectSpanMethod({row, column, rowIndex, columnIndex}) {
    if (columnIndex === 0) {
        // 第一列的合并方法
        const row1 = project_merageArr[rowIndex];
        const col1 = row1 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
        return {rowspan: row1, colspan: col1};
    } else if (columnIndex === 1) {
        // 第二列的合并方法
        const row2 = step_merageArr[rowIndex];
        const col2 = row2 > 0 ? 1 : 0; // 如果被合并了row = 0; 则他这个列需要取消
        return {rowspan: row2, colspan: col2};
    }
}

/**
 * element-ui 合并多列专用
 */
export function merage(data) {
    init();
    tableData = copyArr(data)
    for (let i = 0; i < tableData.length; i++) {
        if (i === 0) {
            // 第一行必须存在
            project_merageArr.push(1);
            project_meragePos = 0;
            step_merageArr.push(1);
            step_meragePos = 0;
        } else {
            // 判断当前元素与上一个元素是否相同,eg：paragraph_meragePos 是 typeNameArr序号
            // 第一列
            if (tableData[i].colName === tableData[i - 1].colName) {
                project_merageArr[project_meragePos] += 1;
                project_merageArr.push(0);
            } else {
                project_merageArr.push(1);
                project_meragePos = i;
            }
            // 第二列
            if (tableData[i].step === tableData[i - 1].step && tableData[i].colName === tableData[i - 1].colName) {
                step_merageArr[step_meragePos] += 1;
                step_merageArr.push(0);
            } else {
                step_merageArr.push(1);
                step_meragePos = i;
            }
        }
    }
    return tableData;
}
